<main class="" th:fragment="warehouseAdmin">
    <body>


    <table id="dg" title="仓库管理" class="easyui-datagrid" style="width:1280px;height:500px"
           url="/warehouse/s"
           toolbar="#toolbar" pagination="true"
           rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
        <tr>
            <th field="id" width="50">仓库ID</th>
            <th field="name" width="50">仓库昵称</th>
            <th field="countNum" width="50">剩余商品类别数</th>
            <th field="countSumNum" width="50">剩余商品总件数</th>
            <th field="address" width="50">联系地址</th>
            <th field="contact" width="50">联系方式</th>
            <th field="remarks" width="50">备注</th>

        </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newwarehouse()">添加仓库</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editwarehouse()">修改仓库</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroywarehouse()">删除仓库</a>
        <div id="tb" style="padding:3px">

            <span>仓库ID:</span>
            <input id="remarks" style="line-height:26px;border:1px solid #ccc">
            <span>仓库昵称:</span>
            <input id="name" style="line-height:26px;border:1px solid #ccc">
            <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">搜索</a>
        </div>
    </div>

    <div id="dlg" class="easyui-dialog" style="width:550px"
         data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
        <form  id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <h3>仓库信息</h3>
            <div style="margin-bottom:10px">
                <input name="name" class="easyui-textbox" required="true" label="仓库昵称:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="address" class="easyui-textbox" required="true" label="联系地址:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="contact" class="easyui-textbox" required="true" label="联系方式:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="remarks" class="easyui-textbox" required="true"  label="备注:" style="width:100%">
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="savewarehouse()"
           style="width:90px">Save</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
    </div>
    <script type="text/javascript">
        var url;
        //搜索
        function doSearch(){
            var data = {};
            if( $('#name').val()){
                data.name = $('#name').val();
            }
            if( $('#remarks').val()){
                data.id = $('#remarks').val();
            }
            $('#dg').datagrid('load',data);
        }

        //添加
        function newwarehouse() {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', '新增仓库');
            $('#fm').form('clear');
            url = '/warehouse/add';
        }
        //修改
        function editwarehouse() {
            var row = $('#dg').datagrid('getSelected');
            if (row) {
                $('#dlg').dialog('open').dialog('center').dialog('setTitle', '修改仓库');
                $('#fm').form('load', row);
                url = '/warehouse/up?id=' + row.id;
            }
        }
        //添加和修改保存仓库
        function savewarehouse() {
            $('#fm').form('submit', {
                url: url,
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (result) {
                    if (result == 'false-has-double') {
                        $.messager.show({
                            title: 'Error',
                            msg: '重名'
                        });
                    } else {
                        var result = eval('(' + result + ')');
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the warehouse data
                    }
                }
            });
        }
        //删除
        function destroywarehouse() {
            var row = $('#dg').datagrid('getSelected');
            if (row) {
                $.messager.confirm('Confirm', '确定要销毁此仓库吗?', function (r) {
                    if (r) {
                        $.post('/warehouse/del', {id: row.id}, function (result) {
                            if (result) {
                                $('#dg').datagrid('reload');    // reload the warehouse data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: "不允许删除，当前仓库下存在商品"
                                });
                            }
                        }, 'json');
                    }
                });
            }
        }
    </script>
    </body>
</main>